# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         utils
# Description:
# Author:       zx
# Date:         2021/4/1
# -------------------------------------------------------------------------------
import csv
import os

import numpy as np

FILE_ROOT = "/data_ann/"
POS_ROOT = "/positive/"
NEG_ROOT = "/negative/"


def request_parse(req_data):
    """解析请求数据并以json形式返回"""
    if req_data.method == 'POST':
        return req_data.json
    elif req_data.method == 'GET':
        return req_data.args
    return None


def get_files(file_name=FILE_ROOT):
    return os.listdir(os.path.dirname(os.path.abspath(__file__)) + file_name)  # 得到文件夹下的所有文件名称


def read_csv(file_name):
    # 读取csv至字典
    with open(os.path.dirname(os.path.abspath(__file__)) + FILE_ROOT + file_name + ".csv", "r", encoding="GBK") as csvFile:
        reader = csv.reader(csvFile)
        sample = np.array(list(reader)[1:]).astype(np.float32)
    return sample


def delete_ann(path):
    if os.path.exists(path):  # 如果文件存在
        # 删除文件，可使用以下两种方法。
        os.remove(path)
